<template>
  <!-- 题库 -->
  <div class="app-container">
    <el-button
      @click="openAddFn"
      type="primary"
      v-hasPermi="['assess:examSubject:add']"
      >新增</el-button
    >
    <div class="boxlaw">
      <div slot="header" class="clearfix">
        <ComSelect @EventChange="ChangeFn" :options="options"></ComSelect>
      </div>

      <div class="comform">
        <ComForm
          ref="comForm"
          @EventReset="resetFn"
          @EventOnsubmit="queryFn"
          :list="comForm"
        >
        </ComForm>
      </div>

      <ComTable
        @handleCurrentChange="CurrentPageChangeFn"
        @handleSizeChange="currentPageSizeChangeFn"
        :key="random"
        :list="tableList"
        :modes="tableModes"
        :config="{ border: true, loading }"
        :pagination="{ total: paging.total, pageNum: paging.pageNum }"
      >
        <el-table-column align="left">
          <template #default="{ row }">
            <el-button
              type="danger"
              @click="deleteFn(row)"
              v-hasPermi="['assess:examSubject:remove']"
              >删除</el-button
            >
            <el-button
              @click="editDialogFn(row)"
              v-hasPermi="['assess:examSubject:getInfo']"
              >修改</el-button
            >
          </template>
        </el-table-column>
      </ComTable>
    </div>
    <AddQuestion
      @EditEvent="editFn"
      v-if="dialogVisible"
      :editId.sync="editId"
      @submitEvent="submitFn"
      :dialogVisible.sync="dialogVisible"
    ></AddQuestion>
  </div>
</template>

<script>
import ComForm from "../../../components/comForm.vue";
import ComSelect from "../../../components/comSelect.vue";
import ComTable from "../../../components/comTable.vue";
import comForm from "../../../components/comForm.vue";
// import { getdocGetTypeApi } from "@/wss/api/ContentSupervision.js";

import {
  getExamSubjectListApi,
  getExamSubjectRemoveApi,
  getExamSubjectAddApi,
  getExamSubjectEditApi,
} from "@/wss/api/HumanResources/EM/qustion.js";
import AddQuestion from "./addQuestion.vue";
export default {
  name: "laws",
  dicts: ["sys_normal_disable"],
  components: { ComSelect, ComForm, comForm, ComTable, AddQuestion },

  data() {
    return {
      // 修改id
      editId: "",
      dialogVisible: false,
      initSelectValue: "",
      // 加载
      loading: false,
      paging: {
        pageNum: 1,
        pageSize: 10,
        postionType: "",
        type: "",
        pattern: "",
        createTime: null,
      },
      // 切换筛选状态，不更新bug修复
      random: "",
      // 当前筛选的状态
      currentSelect: "",
      options: {
        value: "",
        title: "快速筛选",
        list: [
          {
            value: "",
            label: "全部",
          },
          {
            value: "1",
            label: "专家",
          },
          {
            value: "2",
            label: "商务",
          },
        ],
      },
      // 动态生成下拉列表的筛选条件
      dynamicFilter: {},
      // 表格的数据
      tableList: [],
      tableModes: [
        {
          label: "题目ID",
          mode: "no",
        },
        {
          label: "题目",
          mode: "question",
          showOverflowTooltip: true,
        },
        {
          label: "答案",
          mode: "answer",
        },
        {
          label: "状态",
          mode: "status",
          effectiveStatus: true,
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "已发布",
              v: 0,
            },
            {
              l: "草稿",
              v: 1,
              type: "warning",
            },
          ],
        },
        {
          label: "类型",
          mode: "type",
          effectiveStatus: true,
          width: 100,
          align: "center",
          isShowTag: true,
          options: [
            {
              l: "必考",
              v: 1,
              type: "warning",
            },
            {
              l: "非必考",
              v: 2,
            },
          ],
        },
        {
          label: "创建时间",
          mode: "createTime",
        },
      ],
      comForm: {
        modes: [
          {
            placeholder: "请输入题目ID",
            formType: "input",
            mode: "no",
            label: "题目ID",
            value: "",
          },
          {
            placeholder: "请输入试题内容",
            formType: "input",
            mode: "question",
            label: "题目",
            value: "",
          },
          {
            formType: "select",
            mode: "pattern",
            label: "题型",
            value: "",
            select: [],
          },
          {
            formType: "select",
            mode: "type",
            label: "类型",
            select: [],
            value: null,
          },
          {
            placeholder: "请选择日期",
            formType: "date",
            mode: "createTime",
            label: "创建时间",
            value: null,
            pickerType: "datetime",
          },
        ],
      },
    };
  },

  mounted() {
    this.getExamSubjectListFn();
  },
  methods: {
    // 修改对话框
    async editFn(data) {
      console.log(data);
      const { msg, code } = await getExamSubjectEditApi(data);
      if (code == 200) {
        this.getExamSubjectListFn();
        this.$message({
          type: "success",
          message: msg,
        });
      }
    },
    // 修改对话框显示
    editDialogFn(row) {
      this.editId = row.idStr;
      this.dialogVisible = true;
    },
    async submitFn(data) {
      const { msg, code } = await getExamSubjectAddApi(data);
      // console.log();
      if (code == 200) {
        this.dialogVisible = false;
        this.getExamSubjectListFn();
        this.$message({
          type: "success",
          message: msg,
        });
      }
    },
    // 删除函数
    async deleteFn(row) {
      this.$confirm("此操作将删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await getExamSubjectRemoveApi(row.idStr);
          this.getExamSubjectListFn();
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 打开添加对话框
    openAddFn() {
      this.dialogVisible = true;
    },
    // 重设函数
    resetFn() {
      this.initSelectValue = "";
      this.romdom = Math.random();
      this.options.value = "";
      this.paging.postionType = "";
      console.log(this.paging);
    },
    // 当前页发生改变时候
    CurrentPageChangeFn(pageNUm) {
      this.paging.pageNum = pageNUm;
      this.getExamSubjectListFn(this.paging);
    },
    // 页容量发生改变时候
    currentPageSizeChangeFn(pageSize) {
      this.paging.pageSize = pageSize;
      this.getExamSubjectListFn(this.paging);
    },
    // 筛选数据
    queryFn(arr) {
      arr.forEach((item) => {
        for (const key in item) {
          this.paging[key] = item[key];
        }
      });
      this.currentSelect = this.initSelectValue;
      console.log(this.paging, "查询");
      this.paging.pageNum = 1;
      this.getExamSubjectListFn(this.paging);
    },
    // 获取列表数据
    async getExamSubjectListFn(pagination = null) {
      this.loading = true;
      let { rows, code, total } = await getExamSubjectListApi(pagination);

      console.log(rows, "法规返回的数据");
      if (code == 200) {
        this.tableList = rows;
        this.paging.total = total;
        console.log(this.tableList, "题库返回的数据");
        // 把后端放回的字段变成下拉筛选字段
        this.tableList.forEach((item) => {
          //是否有这个属性
          this.dynamicFilter.type ? "" : (this.dynamicFilter.type = []);
          this.dynamicFilter.pattern ? "" : (this.dynamicFilter.pattern = []);
          if (!this.dynamicFilter.pattern.includes(item.pattern)) {
            this.dynamicFilter.pattern.push(item.pattern);
          }

          if (!this.dynamicFilter.type.includes(item.type)) {
            this.dynamicFilter.type.push(item.type);
          }
        });

        this.comForm.modes.forEach((item) => {
          switch (item.mode) {
            case "type":
              item.select = this.arrTransformObj(this.dynamicFilter.type, [
                "",
                "必考",
                "非必考",
              ]);
              break;

            case "pattern":
              item.select = this.arrTransformObj(this.dynamicFilter.pattern, [
                "",
                "单选",
                "多选",
                "判断",
              ]);

              break;
            default:
              break;
          }
        });

        this.loading = false;
      }
    },
    async ChangeFn(value) {
      this.$refs.comForm.resetFn();
      // 更新是状态（如代编辑）
      this.currentSelect = value;

      this.options.value = value;
      this.paging.postionType = value;
      this.getExamSubjectListFn(this.paging);
    },

    // 查看
    async clickLook(row) {
      // // 获取esid
      // let { rows } = await getdocGetTypeApi({ type: "法规" });
      // // console.log(rows, 11111111111);
      // let currentItem = rows.find((item) => {
      //   return row.idStr === item.esId;
      // });
      // console.log(currentItem, "2321312");
      // console.log("row----", row);
      // return;
      // 是否是失败
      if (row.status == 2) {
        this.$router.push({
          path: "/contentPlatform/laws/laws-failReason",
          query: { id: row.idStr },
        });
      } else {
        this.$router.push({
          path: "/contentPlatform/laws/laws-details",
          query: { id: row.idStr },
        });
      }
    },
  },
};
</script>

<style lang ="scss" scoped>
.comform {
  margin-top: 20px;
}
</style>